package com.ymttest.database.sqlwapper;

import com.ymt.core.tool.FormatUtil;
import com.ymt.database.SqlManager;
import com.ymttest.database.model.DGThirdPartyBindInfo;
import com.ymttest.database.model.DGUserExtInfo;
import com.ymttest.database.model.DGUserInfo;
import com.ymttest.database.model.DGWxAppConfig;
import com.ymttest.database.model.DGWxUnionidBindInfo;
import com.ymttest.database.model.dguser.DGLoginLog;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DGUserWapper {
	SqlManager manage;

	public DGUserWapper() {
		manage = new SqlManager("dguser");
	}

	/**
	 * 查询用户信息
	 * 
	 * @return DGUserInfo
	 */
	public List<DGUserInfo> selectDGUserInfo(Integer userId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", userId);
		List<DGUserInfo> dg = (List) manage.getSelectResults(
				"com.ymttest.database.mapping.DGUserMapper.selectDGUserInfo", params);
		manage.commit();
		return dg;
	}
	//selectDGLoginLog
	/**
	 * 查询用户登入信息
	 * 
	 * @return DGUserInfo
	 */
	public List<DGLoginLog> selectDGLoginLog(Integer userId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", userId);
		List<DGLoginLog> dg = (List) manage.getSelectResults(
				"com.ymttest.database.mapping.DGUserMapper.selectDGLoginLog", params);
		manage.commit();
		return dg;
	}
	/**
	 * 查询用户扩展信息
	 * @param userId
	 * @return
	 */
	public List<DGUserExtInfo> selectDGUserExtInfo(Integer userId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", userId);
		List<DGUserExtInfo> dg = (List) manage.getSelectResults(
				"com.ymttest.database.mapping.DGUserMapper.selectDGUserExtInfo", params);
		manage.commit();
		return dg;
	}
	/**
	 * 第三方绑定信息
	 * @param userId
	 * @return
	 */
	public List<DGThirdPartyBindInfo> selectDGThirdPartyBindInfo(Integer userId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", userId);
		List<DGThirdPartyBindInfo> dg = (List) manage.getSelectResults(
				"com.ymttest.database.mapping.DGUserMapper.selectDGThirdPartyBindInfo", params);
		manage.commit();
		return dg;
	}

	public List<DGThirdPartyBindInfo> selectDGThirdPartyOpenId(String openId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("openId", openId);
		List<DGThirdPartyBindInfo> dg = (List) manage.getSelectResults(
				"com.ymttest.database.mapping.DGUserMapper.selectDGThirdPartyOpenId", params);
		manage.commit();
		return dg;
	}

	public List<DGThirdPartyBindInfo> selectDGThirdPartyByUnionId(String unionid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("unionid", unionid);
		List<DGThirdPartyBindInfo> dg = (List) manage.getSelectResults(
				"com.ymttest.database.mapping.DGUserMapper.selectDGThirdPartyByUnionId", params);
		manage.commit();
		return dg;
	}
	/**
	 * unionid绑定信息
	 * @param userId
	 * @return
	 */
	public List<DGWxUnionidBindInfo> selectDGWxUnionidBindInfo(Integer userId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", userId);
		List<DGWxUnionidBindInfo> dg = (List) manage.getSelectResults(
				"com.ymttest.database.mapping.DGUserMapper.selectDGWxUnionidBindInfo", params);
		manage.commit();
		return dg;
	}
	/**
	 * 更新用户状态
	 * @param uid
	 * @param status -1	未激活 <br/>Normal 0	正常<br/>Locked 1	锁定<br/>Close 2	注销
	 * @return
	 */
	public Integer updateDGUserStatus(Integer uid,Integer status) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uid", uid);
		params.put("status", status);
		int count =  manage.update(
				"com.ymttest.database.mapping.DGUserMapper.updateDGUserStatus", params);
		manage.commit();
		return count;
	}
	
	public List<DGWxAppConfig> selectWxAppConfig(String AppName) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("AppName", AppName);
		List<DGWxAppConfig> dg = (List) manage.getSelectResults(
				"com.ymttest.database.mapping.DGUserMapper.selectWxAppConfig", params);
		manage.commit();
		return dg;
	}
	
	public Integer insertWxAppConfig(long BelongSellerId) {
		Map<String, Object> params = new HashMap<String, Object>();
//		params.put("AppName", record.getAppName());
		params.put("AppName", "appsqSeller_" + BelongSellerId);
		params.put("AppId", "wxAppIdAutoTest");
		params.put("Secret", "wxSecretAutotest");
//		params.put("BelongSellerId", record.getBelongSellerId());
		params.put("BelongSellerId", BelongSellerId);
		params.put("UserType", 1);
		int count =  manage.update(
				"com.ymttest.database.mapping.DGUserMapper.insertWxAppConfig", params);
		manage.commit();
		return count;
	}
	
	/**
	 * 关闭数据库连接
	 */
	public void close() {
		manage.close();
		manage = null;
	}

	public static void main(String[] args) throws InterruptedException {
		DGUserWapper wapper = new DGUserWapper();
//		System.out.println(FormatUtil.beanToGSONString(wapper.selectDGUserInfo(500000019)));
//		System.out.println(FormatUtil.beanToGSONString(wapper.selectDGUserExtInfo(500000019)));
//		System.out.println(FormatUtil.beanToGSONString(wapper.selectDGThirdPartyBindInfo(500000019)));
//		System.out.println(FormatUtil.beanToGSONString(wapper.selectDGWxUnionidBindInfo(500000019)));
//		System.out.println(FormatUtil.beanToGSONString(wapper.selectDGThirdPartyOpenId("5c81dfd90044d343e8973d38")));
//		System.out.println(FormatUtil.beanToGSONString(wapper.selectDGLoginLog(500320022)));
		
//		System.out.println(FormatUtil.beanToGSONString(wapper.selectWxAppConfig("appsqSeller_502498791")));
		System.out.println(wapper.insertWxAppConfig(12345L));
	}
}
